<template>
	<!-- 工具办理 申请单-->
	<view>
		<view class="detail-wrap font-35">
			<view class="detail-row">
				<view class="detail-row-label">
					<text>领用人</text>
				</view>
				<view class="detail-row-value ">
					<input :class="orderType!=1?'' :'' " :disabled="orderType==1" type="text" v-model="mainTable.APPLYER" />
				</view>
			</view>
			<view class="detail-row">
				<view class="detail-row-label">
					<text>电话</text>
				</view>
				<view class="detail-row-value">
					<input type="number" class="text-black" :disabled="orderType==1" v-model="mainTable.APPLYERTEL" placeholder="请输入领用人电话" />
				</view>
			</view>

			<view class="detail-row" @click="dateEvent('APPLYERDATE')">
				<view class="detail-row-label">
					<text>借出时间</text>
				</view>
				<view class="detail-row-value" :class="orderType==0?'text-blue' :'' ">
					{{ mainTable.APPLYERDATE }}
				</view>
			</view>

			<view class="detail-row">
				<view class="detail-row-label">
					<text>归还人</text>
				</view>
				<view class="detail-row-value">
					<input type="text" :class="orderType==1?'text-black' :'' " v-model="mainTable.BACKBY" placeholder="请输入归还人" />
				</view>
			</view>
			<view class="detail-row" @click="dateEvent('BACKDATE')">
				<view class="detail-row-label">
					<text>归还时间</text>
				</view>
				<view class="detail-row-value text-blue" >
					{{ mainTable.BACKDATE }}
				</view>
			</view>

			<view class="detail-row">
				<view class="detail-row-label">
					<text>备注</text>
				</view>
				<view class="detail-row-value">
					<input type="text" class="text-black font-35" v-model="mainTable.VMEMO" placeholder="请输入备注" />
				</view>
			</view>
		</view>

		<view class="detail-sub-wrap position-r" v-for="(item,index) in materList">
			
			<view class="text-red delete-cicon" @click="removeEvent(index)">
			  <i class="dicon">&#xe937;</i>
			</view>
			
			<view class="detail-row">
				<view class="detail-row-label">
					<text>工具单号</text>
				</view>
				<view class="detail-row-value text-blue">
					{{ item.PK_MATERIAL || '' }}
				</view>
			</view>
			<view class="detail-row">
				<view class="detail-row-label">
					<text>工具名称</text>
				</view>
				<view class="detail-row-value">
					{{ item.MATERIALNAME  }}
				</view>
			</view>
			<view class="detail-row">
				<view class="detail-row-label">
					<text>规格</text>
				</view>
				<view class="detail-row-value">
					{{ item.MATSPEC  }}
				</view>
			</view>
			
			<view class="detail-row">
				<view class="detail-row-label">
					<text>借出货位</text>
				</view>
				<view class="detail-row-value">
					{{ item.STATIONNAME  }}
				</view>
			</view>
			
			<view class="detail-row position-r" v-if="orderType==1">
				<view class="detail-row-label">
					<text>归还仓库</text>
				</view>
				<view class="detail-row-value" @click="seachEvent('BD_STORE',index)">
					{{ item.VDEF17 || "请选择仓库" }}
				</view>
				<view class="detail-scan-btn" >
					<button type="primary" size="mini" @click="handelScan(index,'BD_STORE')">扫码</button>
				</view>
			</view>
			
			<view class="detail-row position-r" v-if="orderType==1">
				<view class="detail-row-label">
					<text>归还货位</text>
				</view>
				<view class="detail-row-value" @click="seachEvent('BD_RACK',index)">
					{{ item.VDEF18 || '请选货位'   }}
				</view>
				<view class="detail-scan-btn" >
					<button type="primary" size="mini" @click="handelScan(index)">扫码</button>
				</view>
			</view>
			
			<view class="detail-row"  v-if="orderType==1">
				<view class="detail-row-label">
					<text>状态</text>
				</view>
				<view class="detail-row-value">
				 <text :class="item.VDEF20!=1?'text-green':'text-red' ">
					 {{ item.VDEF20 != 1?"正常":'非借出货位归还'  }}
				 </text>	
				</view>
			</view>
			
			<view class="detail-row">
				<view class="detail-row-label">
					<text>库存总量</text>
				</view>
				<view class="detail-row-value">
					{{ item.KCNNUM || '0' }}({{ item.UNIT || '' }})
				</view>
			</view>
			<view class="detail-row">
				<view class="detail-row-label">
					<text>申请数量</text> 
				</view>
				<view class="detail-row-value">
					<!-- LENDNNUM -->
					<input type="number" :disabled="orderType==1" class="text-black" v-model="item.NNUM" @blur="setPoint(index)" placeholder="请输入借出数量" />
				</view>
			</view>
			
			<!-- <view class="detail-row" v-if="orderType==1">
				<view class="detail-row-label">
					<text class="ban-flag">归还数量</text>
				</view>
				<view class="detail-row-value">
					<input type="number" class="text-black" v-model="item.RENNUM" placeholder="请输入归还数量" />
				</view>
			</view>
			
			<view class="detail-row" v-if="orderType==1">
				<view class="detail-row-label">
					<text class="ban-flag">说明</text>
				</view>
				<view class="detail-row-value">
					<input type="text" class="text-black" v-model="item.VMEMO" placeholder="请输入未原位归还的说明" />
				</view>
			</view> -->

		</view>

		<view class="detail-wrap-btn">
			<button class="cu-btn block bg-blue margin-tb-sm lg" @click="submit">
				提交
			</button>
		</view>

		<w-picker mode="date" startYear="2000" endYear="2050" step="1" :defaultVal="[1,1,1,1,2,5]" :current="true" @confirm="onConfirm"
		 ref="dateTime" themeColor="#f00"></w-picker>
	</view>
</template>

<script>
	import {
		mapState,
		mapMutations
	} from 'vuex'
	import wPicker from "@/components/w-picker/w-picker.vue";
	import api from '@/common/api.js'
	let _self, dateTimekey, activeIndex, activeType ;
	
	let query = {
		category: "",
		page: 1,
		rows: 500,
		sidx: "",
		IsReturn:1 ,
		sord: "desc",
		queryJson: {},
		userId: "",
		account: ""
	};
	
	export default {
		computed: mapState(['userInfo', 'checkData']),
		components: {
			wPicker
		},
		data() {
			return {
				orderType: 0, // 0 是借出  1 是归还
				mainTable: {
					BILLNO: '',
					FRBILLNO: '',
					APPLYER: '',
					APPLYERTEL: '',
					BACKBY: '',
					BACKDATE: '',
					VMEMO: '',
					VDEF20: 0 ,
					APPLYTYPE: 0
				},
				materList: []
			};
		},
		onLoad(p) {
			_self = this
			this.orderType = p.t
			this.mainTable.APPLYTYPE = p.t
			this.listenOnce() 
			console.log(this.userInfo);
			if (p.t == 0) {
				this.materList = this.checkData
				this.mainTable.APPLYER = this.userInfo.REALNAME
				this.mainTable.BACKBY = this.userInfo.REALNAME
				this.mainTable.APPLYERDATE = this.$utils.getToDay(0)
				this.mainTable.BACKDATE = this.$utils.getToDay(2)
				this.mainTable.userId = _self.userInfo.UserId
				this.mainTable.account = _self.userInfo.Account
				this.mainTable.APPLYERTEL = _self.userInfo.TELEPHONE
			}else {
				let abc = JSON.parse(decodeURIComponent(p.data));
				this.mainTable = {...abc}
				query.userId = _self.userInfo.UserId;
				query.account = _self.userInfo.Account;
				let a = {PK_OUTSTOCKRECORD:abc.BILLNO  }
				query.queryJson = JSON.stringify(a);
				this.$utils.setPageTitle('工具归还')
				this.getDetail()
			}
		},
		onNavigationBarButtonTap(e) {
			e.index==0?this.addItem():_self.openScanMater()
		},
		methods: {
			...mapMutations([ 'deleteCheckData','setCheckData']),
			
			listenOnce() {
				uni.$on("getSelectItem", function(data) {
					switch (activeType) {
						case "BD_STORE": // 仓库
							_self.materList[activeIndex].VDEF16 = data.BILLNO;
							_self.materList[activeIndex].VDEF17 = data.VNAME;
							break;
						case "BD_RACK": // 货位子
							_self.materList[activeIndex].VDEF18 = data.FULLVNAME;
							_self.materList[activeIndex].VDEF19 = data.BILLNO;
							if(data.BILLNO!= _self.materList[activeIndex].PK_STATION ){
								_self.materList[activeIndex].VDEF20 = 1
							}
							break;
						default:
							break;
					}
				});
			},
			
			
			submit() {
				let URL = '/api/APPCommon/ToolApplicationBill' // api.apiPublic.OutboundRecord;
				// LENDNNUM
				for(var i=0; i<this.materList.length ;i++ ){
					let t = this.materList[i] ;
					if(!t.LENDNNUM && this.orderType!=1){ return this.$utils.tips('请输入申请数量') }
					// if(this.orderType==1 && !t.RENNUM){
					// 	return this.$utils.tips('请输入归还数量')
					// }else if(this.orderType==1 && t.VDEF20 == 1 && !t.VMEMO){
					// 	return this.$utils.tips('请输入未归还原位说明')
					// }
				}
				let savedata = { ...this.mainTable,
					userId:_self.userInfo.UserId,
					PK_APPLYER: _self.userInfo.UserId,
					account : _self.userInfo.Account,
					PK_ORG :  _self.userInfo.ORGANIZEID || '' ,
					DETAILS: JSON.stringify(this.materList)
				}
				// console.log( _self.userInfo);
				// console.log(savedata);
				_self.axios({
						url: URL,
						data: savedata,
						isLoading: true
					})
					.then(res => {
						let {
							IsSuccess,
							Message
						} = res;
						_self.setCheckData([]);
						_self.$utils.tips('提交成功')
						
						setTimeout( ()=>{
							uni.$emit('refreshList', {
								isRefresh: true
							})
							uni.navigateBack({})
						},1600)
					})
			},
			onConfirm(val) {
				this.mainTable[dateTimekey] = val.result;
			},
			
			setPoint(i){
				this.checkData[i].NNUM = Number(this.checkData[i].NNUM).toFixed(2)
			},
			
			removeEvent(i) {
			  let d = this.checkData[i].myIndex + "";
			  if (this.checkData.length == 1) {
			    return this.$utils.tips("最后一项不可删除");
			  }
			  // uni.$emit("deleteEvent", d);
			  this.deleteCheckData(i);
			},
			
			// 选择仓库
			seachEvent(t, i) {
				console.log(555);
				console.log(t,i);
				activeType = t
				activeIndex = i; // 激活的子表下标
				let queryParames = "" , PK_ORG_S='';
				if (t == "BD_RACK" && !this.materList[i].VDEF16) {
					return this.$utils.tips("请先选择仓库");
				}
				if (t == "BD_RACK") {
					queryParames = JSON.stringify({
						PK_STORE: this.materList[i].VDEF16
					});
				}
				console.log(queryParames);
				uni.navigateTo({
					url: "/pages/public/search?t=" + t + "&queryjson=" + queryParames+'&PK_ORG='+PK_ORG_S
				});
			},
			
			
			handelScan(index,t){
				let URL = this.userInfo.BaseURL + '/api/APPCommon/ScanCode';
				// console.log(URL);
				let that = this ;
				this.$utils.Scan(URL, data => {
					if(t == 'BD_STORE'){
						_self.materList[index].VDEF16 = data.PK_STORE
						_self.materList[index].VDEF17   = data.STORENAME 
						return ;
					}
					// console.log(data);
					if(data.PK_STATION != _self.materList[index].PK_STATION ){
						this.mainTable.VDEF20  = 1
						_self.materList[index].VDEF20  = 1 // 非原位归还
					}
					// _self.materList[index].PK_STORE = data.PK_STORE
					// _self.materList[index].STORENAME = data.STORENAME
					_self.materList[index].VDEF18 = data.STATIONNAME 
					_self.materList[index].VDEF19   = data.PK_STATION
					
				})
			},
			
			getDetail() {
				// let URL =  '/api/APPCommon/GetList_OutStcokRecordDTL'; 旧版本
				let URL =  '/api/APPCommon/GetListPage_TC_TOOLRECORD_DTL';
				// console.log(URL);
				// console.log(query);
				_self.axios({
						url: URL,
						data: query,
						isLoading: true
					})
					.then(res => {
						let {
							IsSuccess,
							Message,
							Result
						} = res;
						let Arrays = Result.rows.map(item=>{
							item.RENNUM = ""
							item.VDEF20  = 0
							item.VMEMO = ""
							return item;
						})
						_self.materList = Arrays
						// console.log(Result);
					})
			},

			dateEvent(id) {
				if(this.orderType==1 && id=='APPLYERDATE' ) return;
				dateTimekey = id;
				this.$refs.dateTime.show();
			},
			// 返回
			addItem(){
				uni.navigateBack({})
			},
			// 物资扫描
			openScanMater(){
				let URL =  api.apiPublic.ScanCode;
				this.$utils.Scan(URL, item => {
					item.ischeck = false;
					item.LENDNNUM = '1'; // 借出数量
					item.ENNUM = ''; // 归还数量
					item.TARENNUM = '' // 已归还数量
					item.WAITNNUM = '' // 待归还数量
					let TNT  = [..._self.checkData];
					TNT.push(item)
					_self.setCheckData(TNT);
					_self.materList = _self.checkData
				});
			},
			
		}
	}
</script>

<style lang="scss">
	.text-blue {
		color: #1677ff;
	}
	.detail-scan-btn{
		position: absolute;
		right: 6rpx;
		top: 10rpx;
	}
	.ban-flag::after{
		content: '*';
		color: red;
		display: inline;
		position: absolute;
		left: 128rpx;
	}
	.detail-row-value{
		input{
			font-size: 35rpx !important;
		}
	}
</style>
